package com.wmlive.hhvideo.heihei.personal.presenter;

import android.text.TextUtils;
import com.wmlive.hhvideo.common.base.BasePresenter;
import com.wmlive.hhvideo.common.network.HttpConstant;
import com.wmlive.hhvideo.heihei.login.AccountUtil;
import com.wmlive.hhvideo.heihei.beans.personal.UserHomeResponse;
import com.wmlive.hhvideo.heihei.personal.view.IPersonalView;
import com.wmlive.hhvideo.heihei.beans.login.MobileLoginResponse;
import com.wmlive.hhvideo.heihei.beans.splash.InitCatchData;
import com.wmlive.networklib.entity.DCNetObserver;

/**
 * Created by XueFei on 2017/5/31.
 * <p>
 * 个人及他人主页
 */

public class PersonalPresenter extends BasePresenter<IPersonalView> {
    public PersonalPresenter(IPersonalView view) {
        super(view);
    }

    /**
     * 用户信息
     */
    public void setUserInfo() {
        MobileLoginResponse account = AccountUtil.getAccount();
        if (null != viewCallback) {
            if (null != account) {
                viewCallback.setUserInfoSucceed(account);
            } else {
                viewCallback.setUserInfoFailure();
            }
        }
    }

    /**
     * 更新用户信息
     * @param user_id
     */
    public void getPersonalInfo(long user_id) {
        String url = InitCatchData.userUserHome();
        if (!TextUtils.isEmpty(url)) {
            executeRequest(HttpConstant.TYPE_PERSONAL_HOME, getHttpApi().getUserHome(url, user_id))
                    .subscribe(new DCNetObserver<UserHomeResponse>() {
                        @Override
                        public void onRequestDataReady(int requestCode, String message, UserHomeResponse response) {
                            if (null != viewCallback) {
                                viewCallback.handlePersonalInfoSucceed(response.getUser_info());
                            }
                        }

                        @Override
                        public void onRequestDataError(int requestCode, int serverCode, String message) {
                            if (null != viewCallback) {
                                viewCallback.handlePersonalInfoFailure(message);
                            }
                        }
                    });
        } else if (null != viewCallback) {
            viewCallback.handleNetError();
        }
    }

    /**
     * 重新设置IView
     * @param view
     */
    public void setView(IPersonalView view) {
        if (null == viewCallback) {
            viewCallback = view;
        }
    }
}
